﻿Imports System.Collections.Specialized
Imports Microsoft.VisualBasic.ControlChars

Public Class frmTabKeywords
    Public Shared sKeyTheme As StringCollection = New StringCollection()
    Public Shared sKeyPlace As StringCollection = New StringCollection()
    Public Shared sKeyStratum As StringCollection = New StringCollection()
    Public Shared sKeyTemporal As StringCollection = New StringCollection()
    Public Shared sKdelim As String = CrLf
    Public Shared sPdelim As String = "£"
    Dim editNode As EditXML = New EditXML()

    Private Sub btnThemeAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemeAdd.Click
        Dim sKeyArray() As String
        Dim sList As String
        Dim frmTheme As New frmKeywords("Theme")
        frmTheme.ShowDialog()
        If (frmTheme.getChanged() = True) Then
            sKeyTheme.Add(frmTheme.getKeyDesc()) 'add theme keyword info to collection
            sKeyArray = Split(frmTheme.getKeyDesc(), sPdelim)
            sList = "thesaurus: " & sKeyArray(0) & "   keywords: " & sKeyArray(1)
            sList = sList.Replace(sKdelim, "; ")
            lboxKTheme.Items.Add(sList) 'add formatted info to listbox
        End If
    End Sub

    Private Sub btnThemeEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemeEdit.Click
        Dim sKeyArray() As String
        Dim sList As String
        Dim sToEdit As String
        Dim iCount As Integer = lboxKTheme.Items.Count
        Dim iSelInd As Integer = lboxKTheme.SelectedIndex
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then
            MessageBox.Show("No theme selected for editing", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            sToEdit = sKeyTheme(iSelInd)
            Dim frmTheme As New frmKeywords("Theme", sToEdit)
            frmTheme.ShowDialog()
            If (frmTheme.getChanged() = True) Then
                sKeyTheme.Insert(iSelInd, frmTheme.getKeyDesc()) 'put revised theme keyword info to collection
                sKeyTheme.RemoveAt(iSelInd + 1) 'delete selected theme from collection
                sKeyArray = Split(frmTheme.getKeyDesc(), sPdelim)
                sList = "thesaurus: " & sKeyArray(0) & "   keywords: " & sKeyArray(1)
                sList = sList.Replace(sKdelim, "; ")
                lboxKTheme.Items.Insert(iSelInd, sList) 'put revised formatted info to listbox
                lboxKTheme.Items.RemoveAt(iSelInd + 1) 'delete selected theme from listbox
            End If
        End If
        btnTemporalEdit.Enabled = False
        btnThemeDelete.Enabled = False
    End Sub

    Private Sub btnThemeDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemeDelete.Click
        Dim iSelInd As Integer = 0
        iSelInd = lboxKTheme.SelectedIndex
        If (iSelInd = -1) Then
            MessageBox.Show("No theme selected for deletion", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            sKeyTheme.RemoveAt(iSelInd)  'delete selected theme from collection
            lboxKTheme.Items.RemoveAt(iSelInd)  'delete selected theme from listbox
        End If
        btnTemporalEdit.Enabled = False
        btnThemeDelete.Enabled = False
    End Sub

    Private Sub btnPlaceAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlaceAdd.Click
        Dim sKeyArray() As String
        Dim sList As String
        Dim frmPlace As New frmKeywords("Place")
        frmPlace.ShowDialog()
        If (frmPlace.getChanged() = True) Then
            sKeyPlace.Add(frmPlace.getKeyDesc()) 'add theme keyword info to collection
            sKeyArray = Split(frmPlace.getKeyDesc(), sPdelim)
            sList = "thesaurus: " & sKeyArray(0) & "   keywords: " & sKeyArray(1)
            sList = sList.Replace(sKdelim, "; ")
            lboxKPlace.Items.Add(sList) 'add formatted info to listbox
        End If
    End Sub

    Private Sub btnPlaceEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlaceEdit.Click
        Dim sKeyArray() As String
        Dim sList As String
        Dim sToEdit As String
        Dim iCount As Integer = lboxKPlace.Items.Count
        Dim iSelInd As Integer = lboxKPlace.SelectedIndex
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then
            MessageBox.Show("No place selected", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            sToEdit = sKeyPlace(iSelInd)
            Dim frmPlace As New frmKeywords("Place", sToEdit)
            frmPlace.ShowDialog()
            If (frmPlace.getChanged() = True) Then
                sKeyPlace.Insert(iSelInd, frmPlace.getKeyDesc()) 'put revised theme keyword info to collection
                sKeyPlace.RemoveAt(iSelInd + 1) 'delete selected theme from collection
                sKeyArray = Split(frmPlace.getKeyDesc(), sPdelim)
                sList = "thesaurus: " & sKeyArray(0) & "   keywords: " & sKeyArray(1)
                sList = sList.Replace(sKdelim, "; ")
                lboxKPlace.Items.Insert(iSelInd, sList) 'put revised formatted info to listbox
                lboxKPlace.Items.RemoveAt(iSelInd + 1) 'delete selected theme from listbox
            End If
        End If
        btnPlaceEdit.Enabled = False
        btnPlaceDelete.Enabled = False
    End Sub

    Private Sub btnPlaceDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlaceDelete.Click
        Dim iSelInd As Integer = 0
        iSelInd = lboxKPlace.SelectedIndex
        If (iSelInd = -1) Then
            MessageBox.Show("No place selected for deletion", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            sKeyPlace.RemoveAt(iSelInd)  'delete selected theme from collection
            lboxKPlace.Items.RemoveAt(iSelInd)  'delete selected theme from listbox
        End If
        btnPlaceEdit.Enabled = False
        btnPlaceDelete.Enabled = False
    End Sub

    Private Sub btnStratumAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStratumAdd.Click
        Dim sKeyArray() As String
        Dim sList As String
        Dim frmStratum As New frmKeywords("Stratum")
        frmStratum.ShowDialog()
        If (frmStratum.getChanged() = True) Then
            sKeyStratum.Add(frmStratum.getKeyDesc()) 'add theme keyword info to collection
            sKeyArray = Split(frmStratum.getKeyDesc(), sPdelim)
            sList = "thesaurus: " & sKeyArray(0) & "   keywords: " & sKeyArray(1)
            sList = sList.Replace(sKdelim, "; ")
            lboxKStratum.Items.Add(sList) 'add formatted info to listbox
        End If
    End Sub

    Private Sub btnStratumEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStratumEdit.Click
        Dim sKeyArray() As String
        Dim sList As String
        Dim sToEdit As String
        Dim iCount As Integer = lboxKStratum.Items.Count
        Dim iSelInd As Integer = lboxKStratum.SelectedIndex
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then
            MessageBox.Show("No stratum selected for editing", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            sToEdit = sKeyStratum(iSelInd)
            Dim frmStratum As New frmKeywords("Stratum", sToEdit)
            frmStratum.ShowDialog()
            If (frmStratum.getChanged() = True) Then
                sKeyStratum.Insert(iSelInd, frmStratum.getKeyDesc()) 'put revised theme keyword info to collection
                sKeyStratum.RemoveAt(iSelInd + 1) 'delete selected theme from collection
                sKeyArray = Split(frmStratum.getKeyDesc(), sPdelim)
                sList = "thesaurus: " & sKeyArray(0) & "   keywords: " & sKeyArray(1)
                sList = sList.Replace(sKdelim, "; ")
                lboxKStratum.Items.Insert(iSelInd, sList) 'put revised formatted info to listbox
                lboxKStratum.Items.RemoveAt(iSelInd + 1) 'delete selected theme from listbox
            End If
        End If
        btnStratumEdit.Enabled = False
        btnStratumDelete.Enabled = False
    End Sub

    Private Sub btnStratumDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStratumDelete.Click
        Dim iSelInd As Integer = 0
        iSelInd = lboxKStratum.SelectedIndex
        If (iSelInd = -1) Then
            MessageBox.Show("No stratum selected for deletion", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            sKeyStratum.RemoveAt(iSelInd)  'delete selected theme from collection
            lboxKStratum.Items.RemoveAt(iSelInd)  'delete selected theme from listbox
        End If
        btnStratumEdit.Enabled = False
        btnStratumDelete.Enabled = False
    End Sub

    Private Sub btnTemporalAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTemporalAdd.Click
        Dim sKeyArray() As String
        Dim sList As String
        Dim frmTemporal As New frmKeywords("Temporal")
        frmTemporal.ShowDialog()
        If (frmTemporal.getChanged() = True) Then
            sKeyTemporal.Add(frmTemporal.getKeyDesc()) 'add theme keyword info to collection
            sKeyArray = Split(frmTemporal.getKeyDesc(), sPdelim)
            sList = "thesaurus: " & sKeyArray(0) & "   keywords: " & sKeyArray(1)
            sList = sList.Replace(sKdelim, "; ")
            lboxKTemporal.Items.Add(sList) 'add formatted info to listbox
        End If
    End Sub

    Private Sub btnTemporalEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTemporalEdit.Click
        Dim sKeyArray() As String
        Dim sList As String
        Dim sToEdit As String
        Dim iCount As Integer = lboxKTemporal.Items.Count
        Dim iSelInd As Integer = lboxKTemporal.SelectedIndex
        If (iCount = 1) Then lboxKTemporal.SelectedIndex = 0
        If (iSelInd = -1) Then
            MessageBox.Show("No temporal keyword set selected for editing", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            sToEdit = sKeyTemporal(iSelInd)
            Dim frmTemporal As New frmKeywords("Temporal", sToEdit)
            frmTemporal.ShowDialog()
            If (frmTemporal.getChanged() = True) Then
                sKeyTemporal.Insert(iSelInd, frmTemporal.getKeyDesc()) 'put revised theme keyword info to collection
                sKeyTemporal.RemoveAt(iSelInd + 1) 'delete selected theme from collection
                sKeyArray = Split(frmTemporal.getKeyDesc(), sPdelim)
                sList = "thesaurus: " & sKeyArray(0) & "   keywords: " & sKeyArray(1)
                sList = sList.Replace(sKdelim, "; ")
                lboxKTemporal.Items.Insert(iSelInd, sList) 'put revised formatted info to listbox
                lboxKTemporal.Items.RemoveAt(iSelInd + 1) 'delete selected theme from listbox
            End If
        End If
        btnTemporalEdit.Enabled = False
        btnTemporalDelete.Enabled = False
    End Sub

    Private Sub btnTemporalDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTemporalDelete.Click
        Dim iSelInd As Integer = 0
        iSelInd = lboxKTemporal.SelectedIndex
        If (iSelInd = -1) Then
            MessageBox.Show("No temporal keyword set selected for deletion", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            sKeyTemporal.RemoveAt(iSelInd)  'delete selected theme from collection
            lboxKTemporal.Items.RemoveAt(iSelInd)  'delete selected theme from listbox
        End If
        btnTemporalEdit.Enabled = False
        btnTemporalDelete.Enabled = False
    End Sub

    Private Sub lboxKTheme_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxKTheme.SelectedIndexChanged
        If lboxKTheme.SelectedItems.Count() > 0 Then
            btnThemeEdit.Enabled = True
            btnThemeDelete.Enabled = True
        Else
            btnThemeEdit.Enabled = False
            btnThemeDelete.Enabled = False
        End If
    End Sub

    Private Sub lboxKPlace_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxKPlace.SelectedIndexChanged
        If lboxKPlace.SelectedItems.Count() > 0 Then
            btnPlaceEdit.Enabled = True
            btnPlaceDelete.Enabled = True
        Else
            btnPlaceEdit.Enabled = False
            btnPlaceDelete.Enabled = False
        End If
    End Sub

    Private Sub lboxKStratum_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxKStratum.SelectedIndexChanged
        If lboxKStratum.SelectedItems.Count() > 0 Then
            btnStratumEdit.Enabled = True
            btnStratumDelete.Enabled = True
        Else
            btnStratumEdit.Enabled = False
            btnStratumDelete.Enabled = False
        End If
    End Sub

    Private Sub lboxKTemporal_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxKTemporal.SelectedIndexChanged
        If lboxKTemporal.SelectedItems.Count() > 0 Then
            btnTemporalEdit.Enabled = True
            btnTemporalDelete.Enabled = True
        Else
            btnTemporalEdit.Enabled = False
            btnTemporalDelete.Enabled = False
        End If
    End Sub

    Public Sub editXMLFile()
        editNode.Remove("/metadata/idinfo/keywords", "theme")
        editNode.Remove("/metadata/idinfo/keywords", "place")
        editNode.Remove("/metadata/idinfo/keywords", "stratum")
        editNode.Remove("/metadata/idinfo/keywords", "temporal")
        For i As Integer = sKeyTemporal.Count() To 1 Step -1
            Dim fields As String() = sKeyTemporal(i - 1).Split(sPdelim)
            Dim newNode As String = "<tempkt>" & fields(0) & "</tempkt>"
            Dim keys As String() = fields(1).Split(sKdelim)
            For j As Integer = 0 To keys.GetLength(0) - 1
                newNode = newNode & "<tempkey>" & keys(j).Trim() & "</tempkey>"
            Next
            editNode.Add("/metadata/idinfo/keywords", "temporal", "temporal", newNode)
        Next
        For i As Integer = sKeyStratum.Count() To 1 Step -1
            Dim fields As String() = sKeyStratum(i - 1).Split(sPdelim)
            Dim newNode As String = "<stratkt>" & fields(0) & "</stratkt>"
            Dim keys As String() = fields(1).Split(sKdelim)
            For j As Integer = 0 To keys.GetLength(0) - 1
                newNode = newNode & "<stratkey>" & keys(j).Trim() & "</stratkey>"
            Next
            editNode.Add("/metadata/idinfo/keywords", "stratum", "stratum", newNode)
        Next
        For i As Integer = sKeyPlace.Count() To 1 Step -1
            Dim fields As String() = sKeyPlace(i - 1).Split(sPdelim)
            Dim newNode As String = "<placekt>" & fields(0) & "</placekt>"
            Dim keys As String() = fields(1).Split(sKdelim)
            For j As Integer = 0 To keys.GetLength(0) - 1
                newNode = newNode & "<placekey>" & keys(j).Trim() & "</placekey>"
            Next
            editNode.Add("/metadata/idinfo/keywords", "place", "place", newNode)
        Next
        For i As Integer = sKeyTheme.Count() To 1 Step -1
            Dim fields As String() = sKeyTheme(i - 1).Split(sPdelim)
            Dim newNode As String = "<themekt>" & fields(0) & "</themekt>"
            Dim keys As String() = fields(1).Split(sKdelim)
            For j As Integer = 0 To keys.GetLength(0) - 1
                newNode = newNode & "<themekey>" & keys(j).Trim() & "</themekey>"
            Next
            editNode.Add("/metadata/idinfo/keywords", "theme", "theme", newNode)
        Next
    End Sub

    Private Sub frmTabKeywords_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        editXMLFile()
    End Sub

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("Keywords")
        helpForm.ShowDialog()
    End Sub
End Class